package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.CleanUtils;
import com.blankj.utilcode.util.SPUtils;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.utils.Utils;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerSetUpComponent;
import com.eduhzy.ttw.teacher.mvp.contract.SetUpContract;
import com.eduhzy.ttw.teacher.mvp.presenter.SetUpPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.integration.AppManager;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import io.realm.Realm;
import io.realm.RealmConfiguration;

import static com.jess.arms.utils.Preconditions.checkNotNull;

@Route(path = RouterHub.APP_SETUPACTIVITY)
public class SetUpActivity extends BaseActivity<SetUpPresenter> implements SetUpContract.View {

    @BindView(R.id.version_code)
    TextView versionCode;
    @BindView(R.id.switch_tip)
    Switch mSwitchTip;


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerSetUpComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_set_up; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        versionCode.setText("V" + AppUtils.getAppVersionName());

        boolean tipEnable = SPUtils.getInstance().getBoolean(Constants.TIP_SCHOOL_OR_CLASS, true);
        mSwitchTip.setChecked(tipEnable);
        mSwitchTip.setOnCheckedChangeListener((buttonView, isChecked) -> {
            SPUtils.getInstance().put(Constants.TIP_SCHOOL_OR_CLASS, isChecked, SPUtils.getInstance().contains(Constants.TIP_SCHOOL_OR_CLASS));
        });
    }

    @Subscriber(tag = RouterHub.APP_SETUPACTIVITY)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.UPDATE_TIP_SCHOOL_AND_CLASS:
                boolean tipEnable = SPUtils.getInstance().getBoolean(Constants.TIP_SCHOOL_OR_CLASS, true);
                mSwitchTip.setChecked(tipEnable);
                break;
            default:
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick({R.id.change_password_txt, R.id.clear_cache_layout, R.id.update_layout, R.id.logout_txt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.change_password_txt:
                Utils.navigationWithTitle(RouterHub.APP_CHANGEPASSWORDACTIVITY, Constants.CHANGE_PASSWORD);
                break;
            case R.id.clear_cache_layout:
                QMUITipDialog tip = new QMUITipDialog.Builder(this).setTipWord("请稍后···").setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING).create();
                tip.show();
                if (CleanUtils.cleanExternalCache() && CleanUtils.cleanInternalCache()) {
                    tip.dismiss();
                    RxUtil.showTipDialog(this, QMUITipDialog.Builder.ICON_TYPE_SUCCESS, "清理成功", 1000);
                }
                GSYVideoManager.instance().clearAllDefaultCache(SetUpActivity.this);
                break;
            case R.id.update_layout:
                mPresenter.getAppUpdate();
                break;
            case R.id.logout_txt:
                SPUtils.getInstance().put(Constants.USERID, "");
                SPUtils.getInstance().put(Constants.TOKEN, "");
                ARouter.getInstance().build(RouterHub.APP_LOGINACTIVITY).withBoolean(AppManager.IS_NOT_ADD_ACTIVITY_LIST, true).navigation();
                JPushInterface.setAlias(this, 2, "");
                JPushInterface.stopPush(this);
                Realm.setDefaultConfiguration(new RealmConfiguration.Builder().build());
                ArmsUtils.killAll();
                break;
            default:
        }
    }

    @Override
    public Activity getActivity() {
        return this;
    }
}
